<?php
class PracelPrice extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'shop_pracel_price';
    }
    
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('zone_id, weight, price', 'required'),
            //array('weight, price', 'numerical', 'integerOnly' => true),
            array('weight, price', 'length', 'max' => 10),
            array('weight, price, zone_id', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('weight, price, zone_id', 'safe', 'on' => 'search'),
        );
    }
    
    public function relations()
    {
        return array('zone' => array(self::BELONGS_TO, 'ShopZone', 'zone_id'));
    }
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Products the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }
    
	public function getShippingPrice($countryId, $weight_total) 
        {
            $weight = $weight_total/1000;
            $criteria = new CDbCriteria;
            $criteria->select = 't.* ';
            $criteria->join = ' LEFT JOIN `shop_country` AS `c` ON c.zone_id = t.zone_id';
            $criteria->addCondition("country_id = $countryId AND weight >= $weight");
            $resultSet = $this->model()->find($criteria);

            return $resultSet->price;
        }
}
